package com.lgrousset.loanssimulator.client.chart.widget;

import java.util.List;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.LegendPosition;
import com.google.gwt.visualization.client.Selection;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.events.SelectHandler;
import com.google.gwt.visualization.client.visualizations.corechart.ColumnChart;
import com.google.gwt.visualization.client.visualizations.corechart.Options;
import com.lgrousset.loanssimulator.client.EventBus;
import com.lgrousset.loanssimulator.client.chart.event.LoanChangedEvent;
import com.lgrousset.loanssimulator.shared.model.Loan;

public class LoansVisualization extends Composite {
	private FlowPanel panel = new FlowPanel();
	private ColumnChart areaChart = null;
	private DataTable dataTable = null;
	private Loan loan;

	public LoansVisualization(Loan loanp) {
		this.loan = loanp;
		
		panel.setStyleName("loansVisualization");

		Runnable onLoadCallback = new Runnable() {
			public void run() {
				populateDataTable();
				areaChart = new ColumnChart(dataTable, createOptions());
				//areaChart.addSelectHandler(createSelectHandler(areaChart));
				panel.add(areaChart);
			}
		};
		VisualizationUtils.loadVisualizationApi(onLoadCallback, ColumnChart.PACKAGE);

		LoanChangedEvent.register(EventBus.get(), new LoanChangedEvent.Handler() {
			@Override
			public void onChanged(LoanChangedEvent p) {
				populateDataTable();
				areaChart.draw(dataTable, createOptions());
			}
		});

		initWidget(panel);
	}

	private Options createOptions() {
		Options options = Options.create();
		options.setWidth(800);
		options.setHeight(300);
		options.setIsStacked(true);
		options.setLegend(LegendPosition.TOP);
		return options;
	}

	@SuppressWarnings("unused")
	private SelectHandler createSelectHandler(final ColumnChart chart) {
		return new SelectHandler() {
			@Override
			public void onSelect(SelectEvent event) {
				String message = "";

				// May be multiple selections.
				JsArray<Selection> selections = chart.getSelections();

				for (int i = 0; i < selections.length(); i++) {
					// add a new line for each selection
					message += i == 0 ? "" : "\n";

					Selection selection = selections.get(i);

					if (selection.isCell()) {
						// isCell() returns true if a cell has been selected.

						// getRow() returns the row number of the selected cell.
						int row = selection.getRow();
						// getColumn() returns the column number of the selected
						// cell.
						int column = selection.getColumn();
						message += "cell " + row + ":" + column + " selected";
					} else if (selection.isRow()) {
						// isRow() returns true if an entire row has been
						// selected.

						// getRow() returns the row number of the selected row.
						int row = selection.getRow();
						message += "row " + row + " selected";
					} else {
						// unreachable
						message += "Pie chart selections should be either row selections or cell selections.";
						message += "  Other visualizations support column selections as well.";
					}
				}

				Window.alert(message);
			}
		};
	}

	private void populateDataTable() {
		dataTable = DataTable.create();
		dataTable.addColumn(ColumnType.STRING, "Année");
		dataTable.addColumn(ColumnType.NUMBER, "Capital remboursé");
		dataTable.addColumn(ColumnType.NUMBER, "Intérêts");
		dataTable.addColumn(ColumnType.NUMBER, "Assurance");
		
		//NumberFormat df = NumberFormat.getFormat("###.00");
		//NumberFormat.getCurrencyFormat();
		
		if(dataTable.getNumberOfRows() > 0)
			dataTable.removeRows(0, dataTable.getNumberOfRows()-1);
		
		List<Loan.Schedule> list = loan.getAmortizationSchedule();
		dataTable.addRows(list.size());
		
		for (int i = 0; i < list.size(); i++) {
			dataTable.setValue(i, 0, list.get(i).getLabel());
			dataTable.setValue(i, 1, roundToDecimals(list.get(i).getReturnedAmount(),2));
			dataTable.setValue(i, 2, roundToDecimals(list.get(i).getInterestAmount(),2));
			dataTable.setValue(i, 3, roundToDecimals(list.get(i).getInsuranceAmount(),2));
		}
	}
	public static double roundToDecimals(double d, int c) {
		int temp=(int)((d*Math.pow(10,c)));
		return (((double)temp)/Math.pow(10,c));
		}
}
